home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7323 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  75 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: basic question about pointers and local vars
  5. Date: 16 Feb 1996 22:30:41 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb16153041@qcd.lanl.gov>
  8. References: <genecutl-1602961021230001@dharma.hip.berkeley.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: genecutl@mendel.berkeley.edu's message of 16 Feb 1996 18:18:18 GMT
  13.  
  14. In article <genecutl-1602961021230001@dharma.hip.berkeley.edu>
  15. genecutl@mendel.berkeley.edu (gc) writes: 
  16. <snip>
  17.    main() {
  18.  
  19. It is better style to write `int main (void)' or at least `int
  20. main()', though `main()' is sufficient.
  21.  
  22.    char *string;
  23.    function(string);
  24.    ...
  25.  
  26. If you had just filled up the ..., we could all have helped
  27. you. Never, never, assume that the problem is where you think it is:
  28. show a small complete example.
  29.  
  30.    }
  31.    function(char *string) {
  32.       string = (char *)malloc(...);
  33.  
  34. Calling malloc before declaring it results in undefined
  35. behaviour. Include <stdlib.h> at the top. 
  36.  
  37. Calling it with ... is a syntax error, of course :-)
  38.  
  39.       string[0] = 'a';
  40.       string[1] = 'b';
  41.    }
  42.  
  43.    It doesn't seem like it should be because the value of the pointer itself
  44.    gets changed
  45.    here and I would have thought that that gets lost when control goes back
  46.    to main, but
  47.  
  48. Very good. Correct logic.
  49.  
  50.    it does work when I try it.  So where is the problem and what is the right
  51.    way to do this?
  52.  
  53. And some bad logic. What do you mean by when you try it? If you do
  54. something that is meaningless, very often the system will try to make
  55. the best sense of it: that may or may not be what you expected. It is
  56. wrong when you expect it, it is wrong when you don't. (I could have
  57. been more specific if you had told us in what way it met with your
  58. expectations, by posting a complete program.)
  59.  
  60. The correct way involves using pointer to pointer, or returning a
  61. pointer.
  62.  
  63. And read the FAQ before you post. It is very useful for beginner's
  64. questions: as well as many advanced ones too.
  65.  
  66. Cheers
  67. Tanmoy
  68. --
  69. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  70. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  71. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  72. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  73. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  74. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  75.